#pragma once

#include "KRTextureD3D9.h"

class KRDepthSurfaceD3D9 : public KRBaseTextureD3D9
{
public:
	inline KRDepthSurfaceD3D9(IDirect3DSurface9* Surface):m_DepthStencilSurface(Surface)
	{
	}
	virtual ~KRDepthSurfaceD3D9()
	{
		if (m_DepthStencilSurface)
		{
			m_DepthStencilSurface->Release();
		}
	}
	virtual IDirect3DSurface9* GetDepthSurface()
	{
		return m_DepthStencilSurface;
	}

	IDirect3DSurface9* m_DepthStencilSurface;
};

typedef VSmartPtr<KRDepthSurfaceD3D9> D3D9DepthSurfacePtr;

class KRRenderTargetD3D9 : public VRRenderTarget,public VAllocator
{
	friend class KRenderD3D9;
public:
	KRRenderTargetD3D9(void):m_Composed(FALSE),m_D3DDepthSurface(NULL)
	{
		for (UINT Index = 0; Index < VR_MAX_RT; ++Index)
		{
			m_ColorSurface[Index] = NULL;
		}
	}
	virtual ~KRRenderTargetD3D9(void)
	{
		
	}
	virtual void DeleteThis() { VDelete this; }

	virtual void SetColorSurface(UINT Index, VRTexture* Texture)
	{
		VASSERT(Index < VR_MAX_RT);
		KRTextureD3D9* D3DTexture = (KRTextureD3D9*)Texture;
		m_ColorTexture[Index] = D3DTexture;
	}
	virtual void SetDepthSurface(VRTexture* DepthSurface)
	{
		KRBaseTextureD3D9* D3DTexture = (KRBaseTextureD3D9*)DepthSurface;
		if (D3DTexture)
		{
			VASSERT(D3DTexture->GetDepthSurface());
			m_D3DDepthSurface = D3DTexture->GetDepthSurface();
			m_DepthTexture = D3DTexture;
		}else
		{
			m_DepthTexture = NULL;
			m_D3DDepthSurface = NULL;
		}
	}
	virtual BOOL Compose()
	{
#if !VFINAL
		BOOL Empty = TRUE;
		for (UINT Index = 0; Index < VR_MAX_RT; ++Index)
		{
			if (m_ColorTexture[Index] != NULL)
			{
				Empty = FALSE;
				break;
			}
		}
		if (Empty)
		{
			return FALSE;
		}
#endif
		m_Composed = TRUE;
		return TRUE;
	}
	virtual BOOL IsComposed() const
	{
		return m_Composed;
	}
protected:
	KRTextureD3D9Ptr m_ColorTexture[VR_MAX_RT];
	IDirect3DSurface9* m_ColorSurface[VR_MAX_RT];
	KRBaseTextureD3D9Ptr m_DepthTexture;
	IDirect3DSurface9* m_D3DDepthSurface;
	BOOL m_Composed;

};

class KRRenderTargetD3D9_RenderWindow : public VRRenderTarget, public VAllocator
{
	virtual void DeleteThis() { VDelete this; }
	virtual void SetColorSurface(UINT Index, VRTexture* Texture)
	{
		VASSERT(!"Can't Change Backbuffer!");
	}
	virtual void SetDepthSurface(VRTexture* DepthSurface)
	{
		VASSERT(!"Can't Change Backbuffer!");
	}
	virtual BOOL Compose()
	{
		return FALSE;
	}
	virtual BOOL IsComposed() const
	{
		return TRUE;
	}
public:
	KRRenderTargetD3D9_RenderWindow(IDirect3DSurface9* BackBuffer, IDirect3DSurface9* DepthStencilBuffer)
		:m_BackBuffer(BackBuffer),m_DepthStencilBuffer(DepthStencilBuffer)
	{
		if (m_BackBuffer)
		{
			m_BackBuffer->AddRef();
		}

		if (m_DepthStencilBuffer)
		{
			m_DepthStencilBuffer->AddRef();
		}
	}
	virtual ~KRRenderTargetD3D9_RenderWindow()
	{
		if (m_BackBuffer)
		{
			m_BackBuffer->Release(); 
		}
		if (m_DepthStencilBuffer)
		{
			m_DepthStencilBuffer->Release();
		}
	}
	IDirect3DSurface9* m_BackBuffer;
	IDirect3DSurface9* m_DepthStencilBuffer;
};

class KRenderTargetD3D9_Common : public KRRenderTargetD3D9
{
public:

	KRTextureD3D9* m_Color;
	KRTextureD3D9* m_Depth;
};

class KRRenderTargetD3D9_MRT : public KRRenderTargetD3D9
{

};